package ipfind;


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariProxyConnection;
import db.DbConfigUtil;
import org.apache.commons.lang3.RandomStringUtils;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

public class IpCountryCodeDemo {

    //为了测试数据库查询场景，通过ip地址获取对应的国家地区编码，
    //例子中一共大概12w行，整个的ip地址大概43亿，每一行包括3.5w个ip地址
    public static void main(String[] args) throws IOException {
        //1-4294967295
        Map<Long, Long> rangeMap = getIpData();
//        Map<String,String> ipMap = new LinkedHashMap<>();
//        for(Map.Entry<Long,Long> entry:rangeMap.entrySet()) {
//            ipMap.put(IpUtil.longToIp(entry.getKey()),IpUtil.longToIp(entry.getValue()));
//        }
//
//        FileWriter fw = new FileWriter("G:\\test.json");
//        fw.write(JSON.toJSONString(ipMap));
//        fw.close();
        insertIpData(rangeMap);
    }

    private static void insertIpData(Map<Long, Long> rangeMap) {
        HikariConfig config = DbConfigUtil.getHikariConfig();

        try (HikariDataSource dataSource = new HikariDataSource(config);
            HikariProxyConnection connection = (HikariProxyConnection) dataSource.getConnection()) {
            connection.setAutoCommit(true);
            String sql = "insert into ip_range_country_code(id,country_code,ip_start,ip_end)" +
                    " values(seq_ip_country_id.NEXTVAL,?,?,?)";
            int i = 0;
            int batchInterCount = 10000;
            Set<String> countryCodeSet = new HashSet<>();
            for (int j = 0; j < batchInterCount; j++) {
                String s = RandomStringUtils.randomNumeric(10);
                countryCodeSet.add(s);
            }
            List<String> countryCode = new ArrayList<>(countryCodeSet);
            Random rand = new Random(System.currentTimeMillis());
            try (PreparedStatement prep = connection.prepareStatement(sql)) {
                for (Map.Entry<Long, Long> entry : rangeMap.entrySet()) {
                    prep.setString(1, countryCode.get(rand.nextInt(batchInterCount)));
                    prep.setLong(2, entry.getKey());
                    prep.setLong(3, entry.getValue());
                    if (i != 0 && i % batchInterCount == 0) {
                        prep.executeBatch();
                    }
                    prep.addBatch();
                    i++;
                }
                if (i != batchInterCount) {
                    prep.executeBatch();
                }
            }
        } catch (SQLException e) {
           throw new RuntimeException(e);
        }
    }



    private static Map<Long, Long> getIpData() {
        long start = IpUtil.ipToLong("0.0.0.1");
        long endL = IpUtil.ipToLong("255.255.255.255");
        long ipRange = (endL - start) / 120000;
        System.out.println("ipRange:" + ipRange);
        Map<Long, Long> rangeMap = new LinkedHashMap<>();
        while (start < endL) {
            long end = start + ipRange;
            if(end < endL) {
                rangeMap.put(start,end);
            }
            else {
                rangeMap.put(start,endL);
                break;
            }
            start += ipRange + 1;
        }

        System.out.println("rangeMap:" + rangeMap.size());
        return rangeMap;
    }

}
